package org.deschutter.init;

import java.io.IOException;
import java.util.logging.Level;
import org.apache.log4j.Logger;
import org.deschutter.hibernate.SessionUtil;
import org.deschutter.messagehandlers.RunnableTime;
import org.deschutter.parsers.RunnableParser;
import org.deschutter.stats.Ship;
import org.hibernate.Session;
import org.jibble.pircbot.IrcException;
import org.jibble.pircbot.NickAlreadyInUseException;
import org.jibble.pircbot.PircBot;


import java.math.BigDecimal;
import java.util.HashMap;
import org.deschutter.services.ParserService;
import org.springframework.context.ApplicationContext;

/**
 * User: Berten De Schutter
 * Date: 30-Jun-2009
 * Time: 12:24:10
 */
public class ScimitarBot extends PircBot {

     private ApplicationContext applicationContext;

     private ParserService parserService;

    HashMap<String, Ship> ships = new HashMap<String, Ship>();
    
    final static Logger logger = Logger.getLogger(ScimitarBot.class);

    public ScimitarBot() {


    }

    public void init() {
        try {
            this.setName("Scimitar");
            // Enable debugging output.
            this.setVerbose(true);
            // Connect to the IRC server.
            this.connect("firefly.no.eu.netgamers.org", 8080);
            this.setMode("Scimitar", "+x");
            this.sendMessage("p@cservice.netgamers.org", "login berten zitvlakje");
            // Join the #pircbot channel.
            this.joinChannel("#berten");
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(ScimitarBot.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IrcException ex) {
            java.util.logging.Logger.getLogger(ScimitarBot.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void onMessage(String channel, String sender,
                          String login, String hostname, String message) {


        if (message.equalsIgnoreCase("time")) {
            // listHoney();
            RunnableTime runnabletime = new RunnableTime(message, channel, sender, "pm", new BigDecimal(0), this);
            new Thread(runnabletime).start();
        }
        if (message.equalsIgnoreCase("!join")) {
            joinChannel("#berten");
        }

        // Handle Scan Pasting

        if (message.contains("showscan.pl?scan_id") || message.contains("showscan.pl?scan_grp")) {
           parserService.parse(message);
            // RunnableParser parser = new RunnableParser(message, new BigDecimal(1),this);   //TODO change this to real userid
           // new Thread(parser).start();
        } else {
            String[] msg = message.split(" ");
        }
    }

    @Override
    protected void onInvite(String invitedNick, String inviter, String host, String host2, String channel) {
        super.onInvite(invitedNick, inviter, host, host2, channel);

        if (inviter.equals("P")) {
            // joinChannel(channel);
        }


    }

   public  ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public void setApplicationContext(ApplicationContext applicationContext) {
        ScimitarBotMainNetgamers.applicationContext = applicationContext;
    }

    public ParserService getParserService() {
        return parserService;
    }

    public void setParserService(ParserService parserService) {
        this.parserService = parserService;
    }

}
